package com.sample.multi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by zhaolulun on 2018/9/28.
 *
 * @author zhaolulun
 * @date 2018/09/28
 */
public class WriteFile {
    // region 配置
    /**
     * 日志操作
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(WriteFile.class);
    // endregion 

    // region 依赖
    // endregion 

    // region 公有方法

    /**
     * 线程T1输出1，线程T2输出2，线程T3输出3，
     * 文件1中的数都是123的重复
     * 文件2中的数都是213的重复
     * 文件3中的数都是312的重复
     */
    public static void main(String[] args) throws InterruptedException {
        String prefix = "/Users/zhaolulun/IdeaProjects/sample-template/sample-template-test/src/test/resources/spring/";
        String fileName1 = prefix + "1.txt";
        String fileName2 = prefix + "2.txt";
        String fileName3 = prefix + "3.txt";
        final FileUtils fileUtils1 = new FileUtils(fileName1);
        final FileUtils fileUtils2 = new FileUtils(fileName2);
        final FileUtils fileUtils3 = new FileUtils(fileName3);
        // 清空文件内容
        fileUtils1.clearInfoForFile();
        fileUtils2.clearInfoForFile();
        fileUtils3.clearInfoForFile();

        fileUtils1.sa2.acquire();
        fileUtils1.sa3.acquire();

        fileUtils2.sa1.acquire();
        fileUtils2.sa3.acquire();

        fileUtils3.sa1.acquire();
        fileUtils3.sa2.acquire();


        Runnable t1 = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        fileUtils1.sa1.acquire();
                        fileUtils1.write("A");
                        fileUtils1.sa2.release();

                        fileUtils2.sa2.acquire();
                        fileUtils2.write("A");
                        fileUtils2.sa3.release();

                        fileUtils3.sa1.acquire();
                        fileUtils3.write("A");
                        fileUtils3.sa2.release();
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Runnable t2 = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        fileUtils1.sa2.acquire();
                        fileUtils1.write("B");
                        fileUtils1.sa3.release();


                        fileUtils2.sa1.acquire();
                        fileUtils2.write("B");
                        fileUtils2.sa2.release();


                        fileUtils3.sa2.acquire();
                        fileUtils3.write("B");
                        fileUtils3.sa3.release();
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Runnable t3 = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        fileUtils1.sa3.acquire();
                        fileUtils1.write("C");
                        fileUtils1.sa1.release();

                        fileUtils2.sa3.acquire();
                        fileUtils2.write("C");
                        fileUtils2.sa1.release();

                        fileUtils3.sa3.acquire();
                        fileUtils3.write("C");
                        fileUtils3.sa1.release();
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();
    }
    // endregion 

    // region 私有方法
    // endregion     
}
